package process;

import bean.SensorReading;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2021/11/7 11:27
 */
public class Process1_KeyedProcessFunction {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStream<String> inputStream = env.socketTextStream("192.168.88.106", 7777);

        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });


        dataStream.keyBy("id")
                .process(new MyProcess()).print();

        env.execute();


    }

    public static class MyProcess extends KeyedProcessFunction<Tuple, SensorReading, Integer> {

        ValueState<Long> tsTimerState;

        @Override
        public void open(Configuration parameters) throws Exception {
            tsTimerState = getRuntimeContext()
                    .getState(new ValueStateDescriptor<Long>("ts-timer",Long.class));
        }

        @Override
        public void processElement(SensorReading sensorReading,
                                   Context context, Collector<Integer> collector) throws Exception {
            collector.collect(sensorReading.getId().length());
            /**
             * context，获取时间戳，获取场次，测试输出,定时服务
             */
            context.timestamp();
            context.getCurrentKey();
//            context.output();
            context.timerService().currentProcessingTime();  //处理时间
            context.timerService().currentWatermark();  //获取watermark时间
            context.timerService().registerProcessingTimeTimer(
                    context.timerService().currentProcessingTime() + 1000L);  //注册处理时间

            tsTimerState.update(
                    context.timerService().currentProcessingTime() + 1000L);

//            context.timerService().registerEventTimeTimer(
//                    (sensorReading.getTimestamp() + 10) * 1000);    //注册事件时间
//            context.timerService().deleteProcessingTimeTimer(tsTimerState.value());


        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<Integer> out) throws Exception {
            System.out.println(timestamp + "  定时器触发");
            ctx.getCurrentKey();
//            ctx.output();
            ctx.timeDomain();
        }

        @Override
        public void close() throws Exception {
            tsTimerState.clear();
        }
    }
}
